package com.ytc.controller;


import com.ytc.model.*;
import com.ytc.service.*;
import com.ytc.model.User;
import com.ytc.service.AuthorService;
import com.ytc.util.FileUtil;
import com.ytc.util.RedisKeyCommon;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("author")
public class AuthorController {
    @Autowired
    private AuthorService authorService;
    @Autowired
    private BooklistService booklistService;
    @Autowired
    private BookService bookService;
    @Autowired
    private TypeService typeService;
    @Autowired
    private UserService userService;
    @Autowired
    private KeywordService keywordService;
    @Autowired
    private LetterService letterService;

    //柴晓颖：站内未读短信查询
    @RequestMapping("queryUnreadLetter")
    @ResponseBody
    public String queryUnreadLetter(HttpServletRequest request){
        User user = (User) request.getSession().getServletContext().getAttribute("user");
        List<Letter> letters = letterService.queryUnreadLetter(user.getUId());
        if (letters.size() == 0){
            return "0";
        }else {
            String num = String.valueOf(letters.size());
            return num;
        }
    }

    //柴晓颖：站内短信查询
    @RequestMapping("queryLetter")
    public String queryLetter(Model model, HttpServletRequest request){
        User user = (User) request.getSession().getServletContext().getAttribute("user");
        List<Letter> readLetters = letterService.queryReadLetter(user.getUId());
        List<Letter> unReadLetters = letterService.queryUnreadLetter(user.getUId());
        model.addAttribute("readLetters", readLetters);
        model.addAttribute("unReadLetters", unReadLetters);
        return "author/letters";
    }

    //柴晓颖：作者发送站内短信
    @RequestMapping("sendLetter")
    public String sendLetter(Letter letter){
        letterService.authorSendLetter(letter);
        return "redirect:queryLetter";
    }

    //柴晓颖：修改站内短信阅读状态（已读）
    @RequestMapping("updateLstatusRead")
    public String updateLstatusRead(Integer LId){
        letterService.updateLstatusRead(LId);
        return "redirect:queryLetter";
    }

    //柴晓颖：作者跳转到发送短信页面
    @RequestMapping("toSendLetter")
    public String toSendLetter(){
        return "author/sendLetter";
    }

    //柴晓颖：作者中心首页
    @RequestMapping("authorFirst")
    public String authorFirst(HttpServletRequest request){
        User user = (User) request.getSession().getServletContext().getAttribute("user");
        if (user == null || user.getURoleid() != 3) {
            return "redirect:/user/toLoginAuthor";
        }else {
            return "author/first";
        }
    }

    //柴晓颖：删除书籍
    @RequestMapping("delBook")
    @ResponseBody
    public String delBook(Integer bookid){
        booklistService.delBook(bookid);
        return "删除成功";
    }

    //柴晓颖：跳转数据中心
    @RequestMapping("toBookData")
    public String toBookData(Model model,HttpServletRequest request){
        User user = (User) request.getSession().getServletContext().getAttribute("user");
        //根据useid查询作品
        List<Booklist> booklists = booklistService.queryBookListByAuthor(user.getUId());
        model.addAttribute("bookList",booklists);
        //作品展示
        Author author = authorService.queryAuthorByUid(user.getUId());
        System.out.println(author);
        model.addAttribute("author",author);
        return "author/bookData";
    }

    //柴晓颖：查询收藏量
    @RequestMapping("queryShelfNum")
    @ResponseBody
    public String queryShelfNum(Integer blId){
        Long shelfNum = authorService.queryShelfNum(blId);
        System.out.println(shelfNum);
        String shelf1 = String.valueOf(shelfNum);
        return shelf1;
    }

    //柴晓颖：作者中心顶部页面
    @RequestMapping("authorTop")
    public String authorTop(HttpServletRequest request, Model model){
        User user = (User) request.getSession().getServletContext().getAttribute("user");
        model.addAttribute("user", user);
        return "author/top";
    }


    //柴晓颖：作者中心左边页面
    @RequestMapping("authorLeft")
    public String authorLeft(){
        return "author/left";
    }

    //柴晓颖：作者专区-作品管理，查询作者作品并显示
    @RequestMapping("bookManager")
    public String bookManager(Model model, HttpServletRequest request){
        User user = (User) request.getSession().getServletContext().getAttribute("user");
        List<Booklist> list = booklistService.queryBookListByAuthor(user.getUId());
        model.addAttribute("bookList", list);
        return "author/bookManager";
    }

    //柴晓颖：章节管理
    @RequestMapping("chapterManager")
    public String chapterManager(Integer bookid, Model model,String bookname){
        List<Book> bookChapter = bookService.queryChapter(bookid);
        model.addAttribute("bookChapter", bookChapter);
        model.addAttribute("bookname",bookname);
        model.addAttribute("bookid",bookid);
        //更新章节时查询上一章内容
        String chaptername = bookService.queryLastChaptername(bookid);
        model.addAttribute("chaptername",chaptername);
        return "author/chapterManager";
    }

    //柴晓颖：去新增章节页面
    @RequestMapping("toAddChapter")
    public String toAddChapter(Integer bookid,String bookname, String chaptername, Model model,HttpServletRequest request){
        User user = (User) request.getSession().getServletContext().getAttribute("user");
        //根据userid查作者id
        Long aid = authorService.queryAuthoridByUserid(user.getUId());
        model.addAttribute("aid",aid);
        model.addAttribute("bookname",bookname);
        model.addAttribute("chaptername",chaptername);
        model.addAttribute("bookid",bookid);
        return "author/addChapter";
    }

    //柴晓颖：作者新增章节内容
    @PostMapping("addChapter")
    @ResponseBody
    public void addChapter(Book book){
        book.setBNum(book.getBChapter().length());
        bookService.addChapter(book);
    }

    //柴晓颖：通过章节id查找章节去修改
    @RequestMapping("toUpdate")
    public String toUpdate(String chaptername,Integer bChapterid,Model model,String bookname){
        Book book = bookService.queryChapterByChapterid(bChapterid);
        model.addAttribute("book",book);
        model.addAttribute("bookname",bookname);
        model.addAttribute("chaptername",chaptername);
        return "author/updateChapter";
    }

    //柴晓颖：修改章节内容
    @PostMapping("updateChapter")
    @ResponseBody
    public void updateChapter(Book book){
        book.setBNum(book.getBChapter().length());
        bookService.updateChapter(book);
    }

    //柴晓颖：删除章节
    @RequestMapping("delChapter")
    @ResponseBody
    public String  delChapter(Integer bChapterid,Integer bookid,String bookname){
        bookService.delChapter(bChapterid);
        return "删除成功";
    }

    //柴晓颖：作者申请签约，修改状态
    @RequestMapping("applyQianyue")
    @ResponseBody
    public void applyQianyue(Integer blId){
        booklistService.applyQianyue(blId);
    }

    //柴晓颖：作者预览章节内容
    @RequestMapping("preview")
    public String preview(Integer bChapterid,Model model){
        Book book = bookService.preview(bChapterid);
        model.addAttribute("book",book);
        return "author/preview";
    }


    //柴晓颖：查询小类型
    @RequestMapping("getSmallType")
    @ResponseBody
    public List<Type> getSmallType(Integer pid){
        return typeService.getSmallType(pid);
    }


    //zzm
    @RequestMapping("queryUserById")
    public String queryUserById(HttpServletRequest request,Model model){
        User user = (User) request.getSession().getServletContext().getAttribute("user");
        model.addAttribute("user",user);
        return "author/show_gerenxinxi";
    }
    /**
     * @Author zzm
     * @param model
     * @return
     * 个人信息的修改的回填
     */
    @RequestMapping("toUpdateAuthor")
    public String toUpdateAuthor(Integer uId, Model model){
        User user = userService.queryUserById(uId);
        model.addAttribute("user",user);
        return "author/update_gerenxinxi";
    }
    /**
     * @Author zzm
     * @return
     * 个人信息的修改页面
     */
    @RequestMapping("updateAuthor")
    public String updateAuthor(User user,Model model){
        System.out.println(user);
        userService.updateAuthor(user);
        model.addAttribute("user",user);
        return "author/show_gerenxinxi";
    }
    /**
     * @Author zzm
     * @return
     * 作家专区首页
     */
    //张泽萌：作者中心中间页面
    @RequestMapping("authorCenter")
    public String authorCenter(Model model,HttpServletRequest request){
        User user = (User) request.getSession().getServletContext().getAttribute("user");
        model.addAttribute("user",user);
        //作品数量查询
        Long scount = booklistService.queryScountByUid(user.getUId());
        if(scount!=null){
            model.addAttribute("scount",scount);
        }else {
            model.addAttribute("scount",0);
        }

        //作品字数查询
        Long swords = bookService.querySwords(user.getUId());
        if (swords!=null){
            Double num =  swords/10000.0;
            String words = String.valueOf(num);
            words = words.substring(0,words.indexOf(".") + 2);
            model.addAttribute("words",words);
        }else {
            model.addAttribute("words",0.0);
        }
        //作品展示
        Author author = authorService.queryAuthorByUid(user.getUId());
        System.out.println(author);
        model.addAttribute("author",author);
        return "author/center";
    }

    /* zzm 作者跳转创建新书页面  */
    @RequestMapping("toAddBookList")
    public String toAddBookList(HttpServletRequest request,Model model){
        //查询书籍类型
        List<Type> typeList = typeService.queryBookType();
        model.addAttribute("typeList",typeList);
        User user = (User) request.getSession().getServletContext().getAttribute("user");
        //通过userid去作者表查询作者id.
        Long aid = authorService.queryAuthoridByUserid(user.getUId());
        model.addAttribute("aid",aid);
        return  "author/addBookList";
    }
    //zzm 查询书籍询标签
    @Cacheable(RedisKeyCommon.QUERY_KEYWORDS)
    @RequestMapping("queryKeywords")
    @ResponseBody
    public List<Keyword> queryKeywords(){
        List<Keyword> keyBigList = keywordService.queryKeywords();
        return keyBigList;
    }

    //zzm 新增的具体方法
    @CachePut(cacheNames = RedisKeyCommon.QUERY_BOOK_BY_DTO)
    @RequestMapping("addBookList")
    @ResponseBody
    public void addBookList(Booklist booklist,String kId){
        booklistService.addBookList(booklist,kId);
    }

    //zzm:文件上传
    @RequestMapping("upload")
    @ResponseBody
    public Map<String, String> upload(MultipartFile img){
        String fileUpload = FileUtil.FileUpload(img);
        Map<String,String> map = new HashMap<>();
        map.put("img", fileUpload);
        return map;
    }

}
